package com.brillilab.starter.controller.aio.kit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.pa.kit.SampleModifyPa;
import com.brillilab.domain.pa.kit.SampleSearchPa;
import com.brillilab.domain.po.inventory.InventoryRecord;
import com.brillilab.domain.po.inventory.ReagentInventory;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import com.brillilab.service.core.inventory.IReagentInventoryService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.logic.kit.ReagentLogic;
import com.brillilab.service.logic.kit.SampleLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.handler.RequestPower;
/**
 * 样品列表管理
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/sample")
public class SampleController {

	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private ReagentLogic reagentLogic;
	@Autowired
	private SampleLogic sampleLogic;
	@Autowired
	private IKitService kitService;
	@Autowired
	private ILabMemberService labMemberService;
	@Autowired
	private IInventoryRecordService inventoryRecordService;
	@Autowired
	private IReagentInventoryService reagentInventoryService;
	
	/**
	 * 获取样品列表分页
	 * @param req 试剂搜索请求对象
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public ResponseVo sampleList(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestBody SampleSearchPa pa) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		return ResponseVo.success(sampleLogic.sampleList(pa,urs));
	}
	
	/**
	 * 获取样品信息
	 * @param kitId
	 * @return
	 */
	@RequestMapping(value="/info",method=RequestMethod.GET)
	public ResponseVo sampleInfo(@RequestHeader(name = StarterConstant.TOKEN) String token,@RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),kitId);
		return ResponseVo.success(sampleLogic.getSampleInfo(kitId,urs.getLabMemberId()));
	}
	
	
	/** 
	 * 使用记录列表
	 * @param kitId
	 * @return
	 */
	@RequestMapping(value="/useList",method=RequestMethod.GET)
	public ResponseVo useList(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long kitId, @RequestParam(defaultValue="1")Integer pageNum, @RequestParam(defaultValue="10")Integer pageSize) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),kitId);
		PageVo<InventoryRecord> page = inventoryRecordService.selectList(kitId, pageNum, pageSize);
		return ResponseVo.success(page);
	}
	
	
	/**
	 * 删除前判断是否有库存
	 * 
	 * @param token
	 * @param kitId
	 * @return
	 */
	@RequestPower(LabMemberPowerEnum.reagent)
	@RequestMapping(value = "/isInventory", method = RequestMethod.GET)
	public ResponseVo isInventory(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),kitId);
		int inventoryNum = 0;
		// 1、判断是否有库存
		List<ReagentInventory> inventoryList = reagentInventoryService.selectListByKitId(kitId);
		if (inventoryList != null && inventoryList.size() > 0) {
			// 判断是否是
			boolean isOwner = labMemberService.isOwner(inventoryList.get(0).getLabId(), urs.getLabMemberId());
			if(!isOwner) {
				for (ReagentInventory inventory : inventoryList) {
					// 判断是否有权限删除
					Assert.isTrue(inventory.getLabMemberId() == null || urs.getLabMemberId().equals(inventory.getLabMemberId()),
							"无权限删除");
				}
			}
			inventoryNum = inventoryList.size();
		}
		return ResponseVo.successMap("inventoryNum",inventoryNum);
	}

	/**
	 * 删除样品
	 * 
	 * @param kitId
	 * @return
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.reagent)
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ResponseVo delete(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),kitId);
		reagentLogic.delete(kitId, urs.getId());
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.DELETE);
		return ResponseVo.success();
	}

	/**
	 * 获取样品规格信息
	 * 
	 * @param labId
	 *            实验室ID
	 * @param kitId
	 *            kitId
	 * @return
	 */
	@RequestMapping(value = "/spec", method = RequestMethod.GET)
	public ResponseVo getSampleSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),kitId);
		return ResponseVo.success(sampleLogic.getSampleSpec(kitId));
	}
	
	/**
	 * 修改样品
	 * 
	 * @return 
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.reagent)
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public ResponseVo sampleModify(@RequestHeader(name = StarterConstant.TOKEN) String token,@RequestBody SampleModifyPa pa) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(),pa.getKitId());
		sampleLogic.sampleModify(pa);
		inventoryRecordService.add(urs, pa.getKitId(), InventoryRecordEnum.UPDATE);
		return ResponseVo.success();
	}

}
